package com.example.lib;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.content.Context;
import android.util.Log;

public class Decompress {
	private InputStream _zipFile;
	private String _location;
	Context context;
	String URL;

	public Decompress(String URL, String location, Context context) {

		File file = new File(URL);
		FileInputStream zipFile;
		try {
			zipFile = new FileInputStream(file);
			this.URL = URL;
			_zipFile = zipFile;
			Log.v("book url", URL);
			_location = location;
			this.context = context;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// _dirChecker("");

	}

	public String createBookFolder(String URL) {
		String[] arrs = URL.split("/");
		String mainF = arrs[arrs.length - 1].split("\\.")[0];
		String main = _location + "/" + mainF;
		File fmd = new File(main);
		if (!fmd.exists()) {
			fmd.mkdirs();
			Log.v("Created folder", main);
		}
		return main;
	}

	public void createRoot(String RootDir) {
		Log.v("ROOT", RootDir);
		String now = "";
		String pre = "";
		String[] dirs = RootDir.split("/");
		for (int i = 0; i < dirs.length - 1; i++) {
			if (!dirs[i].equals(""))
				pre = pre + "/" + dirs[i];

		}

		File fpre = new File(pre);
		if (!fpre.exists()) {
			createRoot(pre);
		}

		for (String string : dirs) {
			if (!string.equals(""))
				now = now + "/" + string;
		}

		File fmd = new File(now);
		if (!fmd.exists()) {

			fmd.mkdirs();
		}
		return;

	}

	public String unzip() {
		String bookF = "";
		try {
			try {
				EpubUTIL.createRoot(_location);
				// createRoot(_location);
				bookF = createBookFolder(URL);
				Log.v("Book Folder", bookF);
			} catch (Exception e) {
				Log.v("ROOT Exception", e.toString());
			}

			ZipInputStream zin = new ZipInputStream(_zipFile);
			ZipEntry ze = null;
			String temp = bookF;

			while ((ze = zin.getNextEntry()) != null) {
				Log.v("Decompress", "Unzipping " + ze.getName());

				String dir = ze.getName();
				String[] dirs = dir.split("/");
				temp = bookF;
				String sa = temp;
				if (dirs.length > 1) {
					for (int j = 0; j < dirs.length - 1; j++) {
						File fmd = new File(sa + "/" + dirs[j]);
						sa = sa + "/" + dirs[j];
						if (!fmd.exists()) {
							Log.v("Decompress", dirs[j]);
							fmd.mkdirs();
						}

					}
				}
				temp = bookF;
				if (ze.isDirectory()) {
					// _dirChecker(ze.getName());
				} else {
					File df = new File(temp + "/" + ze.getName());

					// FileOutputStream fout = cw.openFileOutput(
					// _location + ze.getName(), Context.MODE_PRIVATE);
					Log.v("Reading", temp + "/" + ze.getName());
					if (!df.exists()) {
						FileOutputStream fout = new FileOutputStream(temp + "/"
								+ ze.getName());
						int d = 0;
						long sd = 0;
						while ((d = zin.read()) != -1) {
							sd += d;
							fout.write(d);
						}
						Log.v("Decompressed", ze.getName() + " Size : "
								+ String.valueOf(sd));
						fout.close();
					}
					zin.closeEntry();

				}

			}
			zin.close();
			return temp;
		} catch (Exception e) {
			Log.e("Decompress", "UnZIP Exception !!", e);
		}
		return "null";
	}

}
